//
//  NSObject+KLException.m
//  KoalaUtilKit
//
//  Created by Tommy on 15/1/29.
//  Copyright (c) 2015年 Koala Team. All rights reserved.
//

#import "NSObject+KLException.h"
#import "NSObject+KLSwizzle.h"
#import "KLLog.h"
#import <objc/runtime.h>


//#import <dlfcn.h>

//typedef void (*KLObjc_SetForwardHandler)(void *fwd, void *fwd_stret);
//
//void klMsgForwardHandler(id self, SEL sel){
//    NSLog(@"");
//}


 

@implementation NSObject (KLException)

//+ (void)load{
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
////        [self kl_swizzleInstanceSelector:@selector(removeObserver:forKeyPath:context:) withNewSelector:KLHookedSel(removeObserver:forKeyPath:context:)];
////        [self kl_swizzleInstanceSelector:@selector(removeObserver:forKeyPath:) withNewSelector:KLHookedSel(removeObserver:forKeyPath:)];
//    });
//}



//KVO crash
//- (void)KLHookedMethod(removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context) {
//    @try{
//        [self KLHookedMethod(removeObserver:observer forKeyPath:keyPath context:context)];
//    }@catch( NSException *exception) {
//        KLLogError(@"exception:%@",exception);
//    }
//}
//
//- (void)KLHookedMethod(removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath) {
//    @try{
//        [self KLHookedMethod(removeObserver:observer forKeyPath:keyPath)];
//    }@catch( NSException *exception) {
//        KLLogError(@"exception:%@",exception);
//    }
//}

/*
+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
//        [self kl_swizzleInstanceSelector:@selector(doesNotRecognizeSelector:) withNewSelector:KLHookedSel(doesNotRecognizeSelector:)];
//        [self kl_swizzleInstanceSelector:@selector(forwardInvocation:) withNewSelector:KLHookedSel(forwardInvocation:)];
//        [self kl_swizzleInstanceSelector:@selector(forwardingTargetForSelector:) withNewSelector:KLHookedSel(forwardingTargetForSelector:)];
//        [self kl_swizzleInstanceSelector:@selector(methodSignatureForSelector:) withNewSelector:KLHookedSel(methodSignatureForSelector:)];
//        
//       [self kl_swizzleClassSelector:@selector(resolveInstanceMethod:) withNewSelector:KLHookedSel(resolveInstanceMethod:)];
//
//        [self kl_setObjcMsgForwardHander];
    });
}

+ (void)kl_setObjcMsgForwardHander{
    void* dll = dlopen("libobjc.A.dylib", RTLD_NOW);
    if(dll){
        KLObjc_SetForwardHandler func = dlsym(dll, "objc_setForwardHandler");
        if(func){
            (func)(klMsgForwardHandler, klMsgForwardHandler);
        }
    }
}


- (void)KLHookedMethod(doesNotRecognizeSelector:(SEL)aSelector){
//    [self KLHookedMethod(doesNotRecognizeSelector:(SEL)aSelector)];
}

- (id)KLHookedMethod(forwardingTargetForSelector:(SEL)aSelector){
    id obj = [self KLHookedMethod(forwardingTargetForSelector:aSelector)];
    return obj;
}
- (void)KLHookedMethod(forwardInvocation:(NSInvocation *)anInvocation){
   [self KLHookedMethod(forwardInvocation:anInvocation)];
};

- (NSMethodSignature *)KLHookedMethod(methodSignatureForSelector:(SEL)aSelector){
    id sign = [self KLHookedMethod(methodSignatureForSelector:aSelector)];
    if(!sign){
        
        NSLog(@"");
    }
    return sign;
}

- (void*) xxxx{
    
    return NULL;
}

+ (BOOL)KLHookedMethod(resolveInstanceMethod:(SEL)sel){
    BOOL rslv = [self KLHookedMethod(resolveInstanceMethod:sel)];
    return rslv;
}
//+ (BOOL)resolveClassMethod:(SEL)sel{
//    return [super resolveClassMethod:sel];
//}
*/
@end
